<?php
/**
 * Created by PhpStorm.
 * User: jinqiang
 * Date: 2021/4/29
 * Time: 11:14
 */

namespace App\Http\Models\Mysql;


use Illuminate\Database\Eloquent\Model;

class Base extends Model
{
    protected $primaryKey = 'id';

    public $timestamps = false;

    protected $hidden = [];

    public $guarded = [];

    public $connection = 'mysql';

    public function createData($data)
    {
        try {
            $res = $this->create($data);
            if ($res) {
                return ['code' => 0, 'msg' => '提示：创建成功', 'id' => $res->id];
            }
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '提示：创建失败,数据可能已存在,发生了冲突', 'data' => $e->getMessage()];
        }
        return ['code' => -1, 'msg'=>'提示：创建失败,数据可能已存在,发生了冲突'];
    }

    public function updateData($id, $data)
    {
        try{
            if(is_array($id)){
                $this->whereIn('id', $id)->update($data);
            }else{
                $this->where('id', (Int)$id)->update($data);
            }
            return ['code' => 0, 'msg' => '提示：更新成功'];
        }catch (\Exception $e){
            return ['code' => -1, 'msg' => '提示：操作失败', 'data' => $e->getMessage()];
        }
    }

    public function updateDataWhere($whereMap, $data)
    {
        try{
            $conn = $this;
            foreach($whereMap as $column => $value){
                if(isset($value['between'])){
                    $conn = $conn->whereBetween($column, $value['between']);
                }elseif (isset($value['in'])){
                    $conn = $conn->whereIn($column, $value['in']);
                }elseif (isset($value['<>'])){
                    $conn = $conn->where($column, '<>', $value['<>']);
                }else{
                    $conn = $conn->where($column, $value);
                }
            }
            $num = $conn->update($data);
            return ['code' => 0, 'msg' => '提示：更新成功', 'data' => $num];
        }catch (\Exception $e){
            return ['code' => -1, 'msg' => '提示：操作失败', 'data' => $e->getMessage()];
        }
    }

    public function deleteData($id)
    {
        try{
            if(is_array($id)){
                $num = $this->whereIn('id', $id)->delete();
            }else{
                $num = $this->where('id', (Int)$id)->delete();
            }
            return ['code' => 0, 'msg' => '提示：删除成功', 'data' => $num];
        }catch (\Exception $e){
            return ['code' => -1, 'msg' => '提示：操作失败', 'data' => $e->getMessage()];
        }
    }

    public function deleteDataWhere($whereMap)
    {
        try{
            $conn = $this;
            foreach($whereMap as $column => $value){
                if(isset($value['between'])){
                    $conn = $conn->whereBetween($column, $value['between']);
                }elseif (isset($value['in'])){
                    $conn = $conn->whereIn($column, $value['in']);
                }elseif (isset($value['not_in'])){
                    $conn = $conn->whereNotIn($column, $value['not_in']);
                }elseif(isset($value['<'])){
                    $conn = $conn->where($column, '<', $value['<']);
                }else{
                    $conn = $conn->where($column, $value);
                }
            }
            $num = $conn->delete();
            return ['code' => 0, 'msg' => '提示：删除成功', 'data' => $num];
        }catch (\Exception $e){
            return ['code' => -1, 'msg' => '提示：操作失败', 'data' => $e->getMessage()];
        }
    }
}
